
<fieldset class="layui-elem-field layui-field-title">
  <legend>添加用户</legend>
</fieldset>

<form class="layui-form">
    <!-- 姓名，身份证号，电话 -->
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" placeholder="请输入用户姓名"
                class="layui-input" required lay-verify="required">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">身份证号</label>
        <div class="layui-input-inline">
            <input type="text" name="idcard" placeholder="请输入用户身份证号" 
                class="layui-input" required lay-verify="required">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">电话</label>
        <div class="layui-input-inline">
            <input type="text" name="phone" placeholder="请输入用户电话号码"
                class="layui-input" required lay-verify="required">
        </div>
    </div>
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="user-add">立即提交</button>
        </div>
    </div>
    
</form>

<script>
layui.use(['form'], function(){
	var form=layui.form;
	
	//表单提交
	form.on('submit(user-add)', function(data){
		//ajax
		$.ajax({
			url: '/api/user/add',    //请求地址
			data: data.field,    //请求参数
			type: 'post',    //请求方式
			dataType: 'json',    //返回数据类型
			success: function(result){    //请求成功时的回调函数，result是服务器返回的数据
				if (result.code > 0){
					// 添加成功
					layer.confirm('用户添加成功，继续添加还是查看？', 
						{btn: ['继续', '查看']}, 
						function(index){
						layer.close(index);
						$("input[name='name']").val("");
						$("input[name='idcard']").val("");
						$("input[name='phone']").val("");
					}, function(index){
						layer.close(index);
						open('/user/list.html');
					});
				} else {
					//添加失败
					layer.msg(result.msg);
				}
			},
			error: function(){    //请求失败时的回调函数
				layer.msg('请求失败');
			}
		});
		
		return false;
	});
});
</script>
